{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度云api-人脸识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.200b082d631acd6959a55e29996754d6.315360000.1918891284.282335-22869680', 'expires_in': 2592000, 'session_key': '9mzdAqdYk99SDF9TFcVfT5rNcrAPAE75hpoV35wi+SMvclCvMQQdua9/dsN1mcm0eTPkQ6D9EnX6fkn5HyMK3VhAw2r4Fg==', 'access_token': '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': 'f885a4510768c38d01522a17b3ed1bbe'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"QnX6riTlBz5M2zy6oGeIuulz\"\n",
    "client_secret = \"DkmCGPQbSWNcekIrVIGdVF01qprxaf31\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 9905057984355,\n",
       " 'timestamp': 1603543719,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '1ce2bda50341173c3cedd8bd5c45f710',\n",
       "    'location': {'left': 170.46,\n",
       "     'top': 183.34,\n",
       "     'width': 214,\n",
       "     'height': 216,\n",
       "     'rotation': 0},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -6.71, 'pitch': -1.91, 'roll': -1.89},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.33},\n",
       "    'face_type': {'type': 'human', 'probability': 0.94}}]}}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.人脸检测\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://kbizoom.com/wp-content/uploads/2019/01/Jisoo-beauty-1.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "# image_type 图片类型：BASE64；URL；FACE_TOKEN。\n",
    "\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 222204,\n",
       " 'error_msg': 'image download fail',\n",
       " 'log_id': 8445101891011,\n",
       " 'timestamp': 1603543981,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.人脸对比\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://kbizoom.com/wp-content/uploads/2019/01/Jisoo-beauty-1.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"},{\\\"image\\\": \\\"https://www.labelk.net/wp-content/uploads/2019/07/lisa-15-683x1024.jpeg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "# face_type 人脸的类型 LIVE；IDCARD；WATERMARK；CERT；INFRARED。\n",
    "\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 7945452535452,\n",
       " 'timestamp': 1603544360,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#3.创建用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2545157579994,\n",
       " 'timestamp': 1603544548,\n",
       " 'cached': 0,\n",
       " 'result': {'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "  'location': {'left': 186.93,\n",
       "   'top': 99.21,\n",
       "   'width': 121,\n",
       "   'height': 118,\n",
       "   'rotation': 1}}}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#4.人脸注册\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"91cf0a5aa45b0371989e56760b30548c\\\",\\\"image_type\\\":\\\"FACE_TOKEN\\\",\\\"group_id\\\":\\\"group1\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"abc\\\",\\\"quality_control\\\":\\\"LOW\\\",\\\"liveness_control\\\":\\\"NORMAL\\\"}\"\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 223103,\n",
       " 'error_msg': 'user not exist',\n",
       " 'log_id': 8435756589458,\n",
       " 'timestamp': 1603544831,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#5.获取人脸用户列表\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1010555899415,\n",
       " 'timestamp': 1603544820,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#6.删除用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.9ec57ce7dd13f108186ff75af0467f59.2592000.1606123284.282335-22869680' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
